package handler

import (
	service "cms/serivce"
	"engine/cache"
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	sferrors "lib/sf-errors"
)

type GiftPage struct {
	Level    int             `json:"level"`
	Title    string          `json:"title"`
	Price    float64         `json:"price"`
	DayMoney float64         `json:"day_money"`
	Znum     decimal.Decimal `json:"znum"`
	MaxDay   int             `json:"max_day"`
	MaxNum   int             `json:"max_num"`
	RunNum   int             `json:"run_num"`
}

func setPrice(level int, price float64, dayMoney float64, znum float64, maxDay, MaxNum int, title string) *GiftPage {
	giftPage := GiftPage{
		Level:    level,
		Price:    price,
		DayMoney: dayMoney,
		Znum:     decimal.NewFromFloat(znum),
		MaxDay:   maxDay,
		MaxNum:   MaxNum,
		Title:    title,
	}
	return &giftPage
}

func GetGiftPageArr() []*GiftPage {
	return []*GiftPage{}
	giftPageList := make([]*GiftPage, 0, 5)
	giftPageList = append(giftPageList, setPrice(1, 10, 0.3, 12, 40, 5, "一级星途"))
	giftPageList = append(giftPageList, setPrice(2, 100, 2.78, 125.1, 45, 4, "二级星途"))
	giftPageList = append(giftPageList, setPrice(3, 500, 13, 650, 50, 3, "三级星途"))
	giftPageList = append(giftPageList, setPrice(4, 1000, 24.55, 1350.2540, 55, 2, "四级星途"))
	giftPageList = append(giftPageList, setPrice(5, 5000, 116.67, 7000.2, 60, 1, "五级星途"))
	return giftPageList
}

func GetGiftPageArrLq() []*GiftPage {
	giftPageList := make([]*GiftPage, 0, 5)
	giftPageList = append(giftPageList, setPrice(0, 10, 0.3, 12, 40, 5, "体验星途"))
	giftPageList = append(giftPageList, setPrice(1, 10, 0.3, 12, 40, 5, "一级星途"))
	giftPageList = append(giftPageList, setPrice(2, 100, 2.78, 125.1, 45, 4, "二级星途"))
	giftPageList = append(giftPageList, setPrice(3, 500, 13, 650, 50, 3, "三级星途"))
	giftPageList = append(giftPageList, setPrice(4, 1000, 24.55, 1350.2540, 55, 2, "四级星途"))
	giftPageList = append(giftPageList, setPrice(5, 5000, 116.67, 7000.2, 60, 1, "五级星途"))
	return giftPageList
}

func CheckYzm(ctx *gin.Context, mobile, code string) error {
	newStr, err := cache.RedisContextGet[string](ctx, "yzm_"+mobile)
	if err != nil {
		return sferrors.NewStringf("验证码错误")
	}

	sendOldPwdH, err := service.Md5PayPwd(code + "abcdef")
	if err != nil {
		return sferrors.NewStringf("验证码错误")
	}

	if *newStr != sendOldPwdH {
		return sferrors.NewStringf("验证码错误")
	}
	cache.RedisContextDelete(ctx, "yzm_"+mobile)
	return nil
}

func CheckYzmTieqiu(ctx *gin.Context, mobile, code string) error {
	newStr, err := cache.RedisContextGet[string](ctx, "game_yzm_"+mobile)
	if err != nil {
		return sferrors.NewStringf("验证码错误")
	}

	sendOldPwdH, err := service.Md5PayPwd(code + "abcdef")
	if err != nil {
		return sferrors.NewStringf("验证码错误")
	}

	if *newStr != sendOldPwdH {
		return sferrors.NewStringf("验证码错误")
	}
	cache.RedisContextDelete(ctx, "yzm_"+mobile)
	return nil
}
